USE [BMIG_MENSUAL_POST_CADENA]

 -- creating the store procedure
IF EXISTS (SELECT name 
	   FROM   sysobjects 
	   WHERE  name = 'WASP_M0010_INGRESO_PRODUCCION_FINAL_UF'
	   AND 	  type = 'P') 
	DROP PROCEDURE WASP_M0010_INGRESO_PRODUCCION_FINAL_UF
GO

CREATE PROCEDURE [dbo].[WASP_M0010_INGRESO_PRODUCCION_FINAL_UF] AS
begin 
    SELECT DISTINCT 
          TipoRegistro
        , PeriodoPro
        , FechaEnvio
        , NroCorrelativoArchivo
        , TipoMovimiento
        , NroProducto
        , CASE 
                WHEN (CuotasPactadas BETWEEN 3 AND 24) THEN '10329484' 
                WHEN (CuotasPactadas BETWEEN 25 AND 36) THEN '10329483' 
                WHEN (CuotasPactadas BETWEEN 37 AND 60) THEN '10329482' 
          END AS NroPoliza
        , CODAGEN
        , NroCredito
        , RutAsegurado
        , [DV Rut Asegurado]
        , [Nombre Asegurado]
        , DireccionAsegurado
        , CASE 
            WHEN (TelefonoAsegurado = '9999') THEN '' 
            ELSE TelefonoAsegurado 
          END AS TelefonoAsegurado
        , CASE 
            WHEN (CodigoComuna = 9999) THEN '    ' 
            ELSE CodigoComuna 
          END AS CodigoComuna
        , DescripcionComuna
        , CodigoCiudad
        , DescripcionCuidad
        , CASE 
            WHEN (CodigoRegion = 999) THEN '' 
            ELSE CodigoRegion 
          END AS CodigoRegion
        , DescripcionRegion
        , VigenciaDesde
        , VigenciaHasta
        , CuotasPactadas
        , CAST(CAST(PrimaBrutaCaja / 1.19 AS decimal(10, 4)) AS varchar) AS PrimaNetaCaja
        , CAST(PrimaBrutaCaja AS varchar(10)) AS PrimaBrutaCaja
        , CAST(CAST(PrimaBrutaPesos AS decimal(10, 4)) AS VARCHAR) AS Expr1
    into     dbo.ingreso_produccion_UF    
    FROM         dbo.ing_produccion
end    
